1923D - Slimes - CodeForces Solution


binary search binary search binary search binary search binary search binary search

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 3e5 + 1;
int T, n, a[N], cnt[N];
ll s[N];

int read()
{
    int x = 0;
    char c = 0;
    while (!isdigit(c))
        c = getchar();
    while (isdigit(c))
        x = (x << 3) + (x << 1) + (c & 15), c = getchar();
    return x;
}
int main()
{
    T = read();
    while (T--)
    {
        n = read();
        for (int i = 1; i <= n; i++)
            a[i] = read(), s[i] = s[i - 1] + a[i];
        for (int i = 2; i <= n; i++)
            cnt[i] = cnt[i - 1] + (a[i] != a[i - 1]);
        for (int i = 1; i <= n; i++)
        {
            int l = 1, r = i - 1;
            while (l <= r)
            {
                int mid = (l + r) >> 1;
                if (s[i - 1] - s[mid - 1] > a[i] && (a[i - 1] > a[i] || cnt[i - 1] - cnt[mid]))
                    l = mid + 1;
                else
                    r = mid - 1;
            }
            int ans = r > 0 ? i - r : INT_MAX;
            l = i + 1, r = n;
            while (l <= r)
            {
                int mid = (l + r) >> 1;
                if (s[mid] - s[i] > a[i] && (a[i + 1] > a[i] || cnt[mid] - cnt[i + 1]))
                    r = mid - 1;
                else
                    l = mid + 1;
            }
            ans = min(ans, l > n ? INT_MAX : l - i);
            printf("%d ", ans == INT_MAX ? -1 : ans);
        }
        putchar('\n');
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1334B - Middle Class
260C - Balls and Boxes
1554A - Cherry
11B - Jumping Jack
716A - Crazy Computer
644A - Parliament of Berland
1657C - Bracket Sequence Deletion
1657B - XY Sequence
1009A - Game Shopping
1657A - Integer Moves
230B - T-primes
630A - Again Twenty Five
1234D - Distinct Characters Queries
1183A - Nearest Interesting Number
1009E - Intercity Travelling
1637B - MEX and Array
224A - Parallelepiped
964A - Splits
1615A - Closing The Gap
4C - Registration System
1321A - Contest for Robots
1451A - Subtract or Divide
1B - Spreadsheet
1177A - Digits Sequence (Easy Edition)
1579A - Casimir's String Solitaire
287B - Pipeline
510A - Fox And Snake
1520B - Ordinary Numbers
1624A - Plus One on the Subset
350A - TL